package com.example.zwh.computershop.shopping_cart;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.zwh.computershop.R;
import com.example.zwh.computershop.handleDataClient.handleData;

import java.util.List;

import static android.content.ContentValues.TAG;

public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ViewHolder>{

    private static List<Shop> mShopList;
    private Context context;

    private static boolean ischange = true;
    public static boolean isManage = false;

    private static double allMoney = 0.0;

    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView mBrand;
        private ImageView imageView;
        private TextView mName;
        private TextView mPrice;
        private TextView mNum;
        private CheckBox mSel;

        private Button btnIn;
        private Button btnDe;
        private TextView shopText;

        ViewHolder(View view) {
            super(view);
            mBrand = (TextView) view.findViewById(R.id.tv_shop_car_head);
            imageView = (ImageView) view.findViewById(R.id.iv_shop_car_pro);
            mName = (TextView) view.findViewById(R.id.tv_shop_car_name);
            mPrice = (TextView) view.findViewById(R.id.tv_shop_car_price);
            mNum = (TextView) view.findViewById(R.id.etAmount);
            btnIn = (Button) view.findViewById(R.id.btnIncrease);
            btnDe = (Button) view.findViewById(R.id.btnDecrease);
            mSel = (CheckBox) view.findViewById(R.id.cb_shop_car_sel);
            shopText = (TextView) view.findViewById(R.id.tv_shop_car_num);
        }
    }

    public ShopAdapter(Context context, List<Shop> mShopList) {
        this.context = context;
        this.mShopList = mShopList;
    }

    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shopp_car_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);

        //点击头像事件
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: 有效");
            }
        });
        return holder;
    }

    //增加这个函数，如果主活动的选中全选时，将每个CheckBox的状态都改为选中就行
    public static void allSelect(boolean ischecked)
    {
        for(Shop i : mShopList)
        {
            i.setmCheckBox(ischecked);
        }
    }

    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final Shop shop = mShopList.get(position);
        holder.mBrand.setText(shop.getBrand());
        holder.imageView.setImageResource(handleData.getResourceByReflect(shop.getImageUrl()));
        holder.mName.setText(shop.getName());
        holder.mPrice.setText(shop.getPrice() + "￥");
        holder.mNum.setText(String.valueOf(shop.getNum()));
        //显示（点击管理）
        if (isManage) {
            holder.shopText.setVisibility(View.GONE);
            holder.btnDe.setVisibility(View.VISIBLE);
            holder.btnIn.setVisibility(View.VISIBLE);
        } else {
            holder.btnDe.setVisibility(View.GONE);
            holder.btnIn.setVisibility(View.GONE);
            holder.shopText.setVisibility(View.VISIBLE);
        }

        holder.mSel.setChecked(shop.getmCheckBox());
        //加减
        holder.btnDe.setOnClickListener(new View.OnClickListener() {
            int pos = holder.getLayoutPosition();
            @Override
            public void onClick(View v) {
                int num = mShopList.get(pos).getNum();
                if (num != 1) {
                    mShopList.get(pos).setNum(num - 1);
                    notifyDataSetChanged();
                }
            }
        });
        holder.btnIn.setOnClickListener(new View.OnClickListener() {

            int pos = holder.getLayoutPosition();
            public void onClick(View v) {
                int num = mShopList.get(pos).getNum();
                mShopList.get(pos).setNum(num + 1);
                notifyDataSetChanged();
            }
        });
        //点击单选按钮
        holder.mSel.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                CheckBox all = (CheckBox) v;
                mShopList.get(position).setmCheckBox(all.isChecked());
                ShoppCar_Fragment.myAllCheckBoxChange();
                ShoppCar_Fragment.setAllMoney(getMoney());
                notifyDataSetChanged();
            }
        });
    }

    private double getMoney() {
        allMoney = 0.0;
        for (Shop i : mShopList) {
            if (i.getmCheckBox())
                allMoney += i.getPrice() * i.getNum();
        }
        return allMoney;
    }

    public static void setIsManage(boolean isManage) {
        ShopAdapter.isManage = isManage;
    }

    public static void setIschange(boolean ischange) {
        ShopAdapter.ischange = ischange;
    }

    public int getItemCount() {
        return mShopList.size();
    }
}

